package com.cheng.xbsc.config;

import com.cheng.xbsc.interceptor.LoginInterceptor;
import com.google.code.kaptcha.servlet.KaptchaServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: 刘文铖
 */
@Configuration
public class KaptchaConfig implements WebMvcConfigurer {

    @Value("${requestPath}")
    private String requestPath;

    @Value("${filePath}")
    private String filePath;

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Bean
    public ServletRegistrationBean<KaptchaServlet> kaptchaServlet() {
        //设置验证码的路径
        ServletRegistrationBean<KaptchaServlet> registrationBean = new ServletRegistrationBean<>(new KaptchaServlet(), "/code.jpg");
        registrationBean.addInitParameter("kaptcha.textproducer.char.length", "4");
        registrationBean.addInitParameter("kaptcha.image.width", "130");
        registrationBean.addInitParameter("kaptcha.image.height", "50");
        registrationBean.addInitParameter("kaptcha.textproducer.font.color", "black");
        registrationBean.addInitParameter("kaptcha.background.clear.from", "yellow");
        registrationBean.addInitParameter("kaptcha.background.clear.to", "green");
        registrationBean.addInitParameter("kaptcha.noise.color", "white");
        registrationBean.addInitParameter("kaptcha.session.key", "checkCode");
        registrationBean.addInitParameter("kaptcha.textproducer.font.size", "35");
        registrationBean.addInitParameter("kaptcha.textproducer.char.string", "1234567890");
        registrationBean.addInitParameter("kaptcha.textproducer.font.names", "Monospaced");
        registrationBean.addInitParameter("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
        return registrationBean;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(requestPath).addResourceLocations("file:" + filePath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/register.html")
                .excludePathPatterns("/changePwd.html")
                .excludePathPatterns("/wxLogin.html")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/fonts/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/xbjs/**")
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/user/checkUsername/**")
                .excludePathPatterns("/user/checkEmail/**")
                .excludePathPatterns("/user/register/**")
                .excludePathPatterns("/wx/toWxLogin/**")
                .excludePathPatterns("/wx/wxLogin/**")
                .excludePathPatterns("/wx/getWxUserInfo/**")
                .excludePathPatterns("/user/sendCode/**")
                .excludePathPatterns("/user/forget/**")
                .excludePathPatterns("/user/login/**")
                .excludePathPatterns("/user/loginOut/**");
    }
}
